package com.ruc.dbiir.rest.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruc.dbiir.rest.utils.Config;
import com.ruc.dbiir.rest.utils.ParseUtil;
import com.ruc.dbiir.rest.utils.ReadFileUtil;


@Controller
@RequestMapping(value = "/highEchart")
public class HignChartsController {




	/**
	 * 获取节点文件
	 * author：mark   
	 * createTime：Sep 7, 2018 4:54:46 PM
	 * @return
	 */
	@RequestMapping(value = "/getGraphNode")
	@ResponseBody
	public Object getGraphNode(@RequestParam(value = "graphName") String graphName) {

		System.out.println("getGraphNode:"+graphName);
		String filePath = Config.RESULT_PATH+graphName+"/g1_new_nodes_as_files_item.csv";
		//		String filePath = "/Users/mark/Documents/MasterLife/GraphDB/neo4j-community-3.4.6/import/Wechatbom_item.csv";
		List<String> list  = ReadFileUtil.readTxtFileIntoStringArrList(filePath);
		//		System.out.println(list.toString());
		return list;
	}

//	
	
	
	
	
	/**
	 * 获取边数据
	 * author：mark   
	 * createTime：Sep 7, 2018 4:54:58 PM
	 * @return
	 */
	@RequestMapping(value = "/getGraphEdge")
	@ResponseBody
	public Object getGraphEdge(@RequestParam(value = "graphName") String graphName) {
		String filePath = Config.RESULT_PATH+graphName+"/g1_new_nodes_as_files_relation.csv";
		//		String filePath = "/Users/mark/Documents/MasterLife/GraphDB/neo4j-community-3.4.6/import/Wechatbom_relation.csv";
		List<String> list  = ReadFileUtil.readTxtFileIntoStringArrList(filePath);
		//		System.out.println(list.toString());
		return list;
	}



	//	@RequestMapping(value = "/startGens")
	//	@ResponseBody
	//	public Object genConf( @RequestParam(value = "username") String username,  @RequestParam(value = "passwd") String passwd,
	//			@RequestParam(value = "nodetype") String nodetype,@RequestParam(value = "edgetype") String edgetype,
	//			@RequestParam(value = "querysize") String querysize,@RequestParam(value = "graphsize") String graphsize) {
	//		System.out.println("genConf Parameters:"+username+passwd+nodetype+edgetype+querysize+graphsize);
	//		
	////		try {
	////			graphsize = URLEncoder.encode(graphsize, "UTF-8");
	////			System.out.println(graphsize);
	////		} catch (UnsupportedEncodingException e1) {
	////			e1.printStackTrace();
	////		}
	//		
	//		String url = Config.GRAPH_ADDR+"/startGen?"+username+"&"+passwd
	//				+"&"+nodetype+"&"+edgetype+"&"+querysize+"&"+graphsize;
	//		System.out.println("url:"+url);
	//		String resultStr = "";
	//		try {
	//			ClientHttpRequest request  = new SimpleClientHttpRequestFactory().createRequest(new URI(url), HttpMethod.GET);
	//			ClientHttpResponse response = request.execute();
	//			
	//			if (response.getStatusCode()==HttpStatus.OK) {
	//				System.out.println("return ok");
	//			}
	//			InputStream is = response.getBody();
	//			byte bytes[] = new byte[(int)response.getBody().available()];
	//			is.read(bytes);
	//			resultStr = new String(bytes, Charset.forName("utf-8"));
	//			System.out.println(resultStr);
	//		} catch (IOException e) {
	//			resultStr = e.getMessage();
	//			e.printStackTrace();
	//		} catch (URISyntaxException e) {
	//			resultStr = e.getMessage();
	//			e.printStackTrace();
	//		}
	//
	//		return ParseUtil.toObject(resultStr);
	//	}


	/**
	 * 向生成器发送请求进行数据生成
	 * author：mark   
	 * createTime：Sep 7, 2018 4:55:58 PM
	 * @param username
	 * @param passwd
	 * @param nodetype
	 * @param edgetype
	 * @param querysize
	 * @param graphsize
	 * @return
	 */

	//	username:data.field.username, passwd:data.field.passwd, 
	//	nodetype:data.field.nodetype,edgetype:data.field.edgetype,
	//	querysize:data.field.querySize,graphsize:data.field.graphSize,
	//	graphLayer:data.field.graphLayer,graphNum:data.field.graphNum

	@RequestMapping(value = "/startGen")
	@ResponseBody
	public Object urlcon( @RequestParam(value = "username") String username,  @RequestParam(value = "passwd") String passwd,
			@RequestParam(value = "nodetype") String nodetype,@RequestParam(value = "edgetype") String edgetype,
			@RequestParam(value = "querySize") String querySize,@RequestParam(value = "graphSize") String graphSize,
			@RequestParam(value = "graphLayer") String graphLayer,@RequestParam(value = "graphNum") String graphNum) {

		HttpURLConnection urlCon = null;
		StringBuilder result = null;
		try {
			String urls = Config.PYSERVER_ADDR+"/startGen?"+username+"&"+passwd
					+"&"+nodetype+"&"+edgetype+"&"+querySize+"&"+graphSize+"&"+graphLayer+"&"+graphNum;
			System.out.println(urls);
			URL url = new URL(urls);
			urlCon = (HttpURLConnection)url.openConnection();
			urlCon.setConnectTimeout(300 * 1000);
			urlCon.setReadTimeout(300 * 1000);
			urlCon.setRequestMethod("GET");
			InputStream is =  urlCon.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String inputLine=null;
			result = new StringBuilder("");
			if((inputLine = br.readLine()) != null){
				System.out.println(inputLine);
				result.append(inputLine+"<br>");
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(result);
		urlCon.disconnect();
		return ParseUtil.toObject(result.toString());
	}


	/**
	 * 获取内容列表
	 * author：mark   
	 * createTime：Sep 7, 2018 4:56:20 PM
	 * @return
	 */
	@RequestMapping(value = "/getContentList")
	@ResponseBody
	public Object getContentList() {

		String command = "sh " + Config.CMD_ADDR_LIST;
		final List<String> processList = new ArrayList<String>(); 

		try {  
			final Process process = Runtime.getRuntime().exec(command);  	
			BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));  
			String line = "";  
			while ((line = input.readLine()) != null) {  
				line = line+"/suf";System.out.println(line);
				processList.add(line.split("/")[0]+"&"+line.split("/")[1]); 
			}  
			input.close(); 
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		for (String line : processList) {  
			System.out.println(line);  
		}  
		return processList;
		//		return ParseUtil.toObject("sss");
	}


	/**
	 * 自动化进行测试
	 * author：mark   
	 * createTime：Sep 7, 2018 4:56:20 PM
	 * @return
	 */
	@RequestMapping(value = "/testPerformance")
	@ResponseBody
	public Object testPerformance(@RequestParam(value = "graphName") String graphName,@RequestParam(value = "dbPath") String dbPath) {

		//		System.out.println(graphName+"dddd"+dbPath);
		//		String shpath="/Users/mark/program/proWorkspace/pyCharmSrc/GenGraph/result/Execute_Command.sh";//.sh文件的绝对路径
		String command = "sh " + Config.RESULT_PATH+graphName+"/Execute_Command.sh "+dbPath;

		final List<String> processList = new ArrayList<String>(); 

		try {  

			final Process process = Runtime.getRuntime().exec(command);  	
			BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));  
			String line = "";  
			while ((line = input.readLine()) != null) {  
				processList.add(line); 
			}  
			input.close(); 

		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		for (String line : processList) {  
			System.out.println(line);  
		}  
		return processList;
		//		return ParseUtil.toObject("sss");
	}

	/**
	 * 添加数据库
	 * author：mark   
	 * createTime：Sep 7, 2018 4:56:20 PM
	 * @return
	 */
	@RequestMapping(value = "/addDatabase")
	@ResponseBody
	public Object addDatabase(@RequestParam(value = "dbName") String dbName,@RequestParam(value = "dbType") String dbType,
			@RequestParam(value = "dbPath") String dbPath) {
		//		System.out.println("adddddddddd----------dbdb");
		String path = Config.RESULT_PATH+"dbCollect.csv";
		String content = dbName+","+dbType+","+dbPath+"\n";
		boolean append = true;
		ReadFileUtil.directFileWriter(path, content, append);
		return ParseUtil.toObject("success");
	}

	/**
	 * 获取数据库
	 * author：mark   
	 * createTime：Sep 7, 2018 4:56:20 PM
	 * @return
	 */
	@RequestMapping(value = "/getDatabase")
	@ResponseBody
	public Object getDatabase() {
		String path = Config.RESULT_PATH+"dbCollect.csv";
		List<String> dbList = ReadFileUtil.readFile(path);
		for (int i = 0; i < dbList.size(); i++) {
			System.out.println(dbList.get(i));
		}
		return dbList;
	}


	@RequestMapping(value = "/getDatabaseList")
	@ResponseBody
	public Object getDatabaseList() {
		String path = Config.RESULT_PATH+"dbCollect.csv";
		JSONArray  jsonArray = new JSONArray();
		JSONObject jObj = new JSONObject();
		List<String> dbList = ReadFileUtil.readFile(path);
		for (int i = 0; i < dbList.size(); i++) {
			System.out.println(dbList.get(i));
			String dbStr = dbList.get(i);
			String arr[] = dbStr.split(",");
			JSONObject jObject = new JSONObject();
			System.out.println(arr[0]+"|"+arr[1]);
			jObject.put("dbName", arr[0]);
			jObject.put("dbType", arr[1]);
			jObject.put("dbPath", arr[2]);
			jsonArray.add(jObject);
		}
		jObj.put("data", jsonArray);
		jObj.put("code", 0);
		jObj.put("msg", "");
		jObj.put("count", 1000);
		return jObj;
	}

//	public static Object getDatabaseList1() {
//		String path = Config.RESULT_PATH+"dbCollect.csv";
//		JSONArray  jsonArray = new JSONArray();
//		JSONObject jObj = new JSONObject();
//		List<String> dbList = ReadFileUtil.readFile(path);
//		for (int i = 0; i < dbList.size(); i++) {
//			System.out.println(dbList.get(i));
//			String dbStr = dbList.get(i);
//			String arr[] = dbStr.split(",");
//			JSONObject jObject = new JSONObject();
//			jObject.put("dbName", arr[0]);
//			jObject.put("dbPath", arr[1]);
//			jsonArray.add(jObject);
//			
//		}
//		jObj.put("data", jsonArray);
//		return jObj;
//	}

	public static Object testPerformance2() {

		//		String shpath="/Users/mark/program/proWorkspace/pyCharmSrc/GenGraph/result/Execute_Command.sh";//.sh文件的绝对路径
		//		String command = "sh /Users/mark/Desktop/tmp/test.sh /Users/mark/Documents/MasterLife/GraphDB/neo4j-community-3.4.6";
//		/Users/mark/program/proWorkspace/pyCharmSrc/GenGraph/result/result_2019101022310
		String command = "sh " + Config.RESULT_PATH+"result_2019101022310/getSub.sh /Users/mark/Documents/MasterLife/GraphDB/neo4j-community-3.4.6/bin";
		final List<String> processList = new ArrayList<String>(); 

		try {  

			final Process process = Runtime.getRuntime().exec(command);  	
			BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));  
			String line = "";  
			while ((line = input.readLine()) != null) {  
				processList.add(line); 
			}  
			input.close(); 

		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		for (String line : processList) {  
			System.out.println(line);  
		}  
		return processList;
		//		return ParseUtil.toObject("sss");
	}


	public static void main(String[] args) {
				testPerformance2();
		//		addDatabase();///////////////////////////
		//		getDatabase2();
//		getDatabaseList1();
	}


}
